<?php
/**
 *  @author: Pham Van Diep
 *  @date:   18/2/2014
 *  @status: 0-> inactive
 *           1->active
 *           2->delete ( not delete out database)
 * 
 **/

if( !isset($AppUI) || 
	!isset($_SESSION['personal'])
) {
	redirect(HOST_BACKEND);
}
$intAction 		= $this->input->post('paction', 0);
$arrpersonalId 	= $this->input->post('ppersonalid', null);
$strError		= '';
$strMsg			= '';

if($intAction && $arrpersonalId) {
	switch ($intAction) {
        case 5: // inactive                      
            if(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<5) != 0) {							
				foreach($arrpersonalId as $row) {
                    //echo $row;
					$intError  = $this->personal->updateStatus($AppUI->id,$row,0,1<<2);
				}
				$strMsg		= $arrMessage['personal_update'];                
			}
			else {
				$strError	= $arrError['noperm_personal_update'];
			}
			break;
		case 1: // active
			if(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<5) != 0) {							
				foreach($arrpersonalId as $row) {
                    //echo $row;
					$intError  = $this->personal->updateStatus($AppUI->id,$row,1,1<<2);
				}
				$strMsg		= $arrMessage['personal_update'];
			}
			else {
				$strError	= $arrError['noperm_personal_update'];
			}
			break;        
		case 2: // member,leader,admin delete
			if(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<2) != 0) {                
                switch($AppUI->type){
                    case ROLE_LEADER:
                        $type = 5;
                        break;
                    case ROLE_ADMINISTRATOR:
                        $type=6;
                        break;
                    case ROLE_MEMBER:
                        $type=4;
                }
                if(! isset($type)) break;				
				foreach($arrpersonalId as $row) {
					$this->personal->updateStatus($AppUI->id,$row,$type,1<<2);
				}
				$strMsg		= $arrMessage['personal_delete'];
			}
			else {
				$strError	= $arrError['noperm_personal_delete'];
			}
			break;	
        case 3: //suspend
            	if(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<2) != 0) {                				
				foreach($arrpersonalId as $row) {
					$this->personal->updateStatus($AppUI->id,$row,2,1<<2);
				}
				$strMsg		= $arrMessage['personal_suspend'];
			}
			else {
				$strError	= $arrError['noperm_personal_delete'];
			}
			break;	
       
		case 4: // delete database
			if(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<3) != 0 && $AppUI->type == ROLE_ROOT) {
				foreach($arrpersonalId as $row) {
					$this->personal->delete($AppUI->id,$row);
				}
				$strMsg		= $arrMessage['personal_deletedb'];
			}
			else {
				$strError	= $arrError['noperm_personal_delete'];
			}
			break;         
		default:
            break;
		
	}
} else {
	$strError	= $arrError['nodata_act'];
}
if($strError == '') {
	CI_Function::setMsg($strMsg);
} else {
	CI_Function::setError($strError);	
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);

